
import java.awt.Graphics;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author 212010509
 */
public class Triangle extends GeoDrawable {
    Line[] m_bisector;

    public Triangle() {
        super();
        m_bisector=new Line[3];
    }

    @Override
    public boolean pressed(int[] point) {

        Handle sh = handleContains(point);
        if (sh == null) {
            if (m_handles.size() < 3) {
                Handle newHandle = new Handle(point);
                m_handles.add(newHandle);
                return true;

            } else {
                return false;
            }
        } else {
            return super.pressed(point);

        }

    }

    @Override
    public void released(int[] point) {
        super.released(point);

    }

    @Override
    public void drag(int[] point) {
        super.drag(point);

    }

    public void line(Graphics g, int[] v1, int[] v2,int lineindex) {
        int x1 = v1[0];
        int y1 = v1[1];
        int x2 = v2[0];
        int y2 = v2[1];
        g.drawLine(x1, y1, x2, y2);
        int midx = ((x2+x1)/2); 
        int midy = ((y2+y1)/2);
        g.drawArc(midx-3, midy-3, 6, 6, 0, 180);
        float[] end1=new float[2];
        float[] end2=new float[2];
        end1[0]=midx;
        end1[1]=midy;
        Line base = new Line(v1,v2);
        float[] deltas=base.deltas();
        float invdx=deltas[1];
        float invdy=-deltas[0];
        float[] perpend=new float[2];
        perpend[0]=midx+invdx;
        perpend[1]=midy+invdy;
        Line perp = new Line (perpend,end1);
        m_bisector[lineindex]=perp;
                
        
    }


    @Override
    public void draw(Graphics g) {
        super.draw(g);
        if (m_handles.size() == 3) {
            Handle h1 = m_handles.get(0);
            Handle h2 = m_handles.get(1);
            Handle h3 = m_handles.get(2);
            line(g, h1.m_coord, h2.m_coord,0);
            line(g, h2.m_coord, h3.m_coord,1);
            line(g, h3.m_coord, h1.m_coord,2);
            float[] tricenter= m_bisector[0].intersects(m_bisector[1]);
            float radius=(float)Math.sqrt((h1.m_coord[0]-tricenter[0])*(h1.m_coord[0]-tricenter[0])+
                    (h1.m_coord[1]-tricenter[1])*(h1.m_coord[1]-tricenter[1]));
            g.drawArc((int) (tricenter[0]-radius),(int)( tricenter[1]-radius),(int) (2*radius),(int) (2*radius), 0, 360);

        }
    }
}
